package app.fugema.shop.helper;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import app.fugema.shop.R;
import app.fugema.shop.databinding.ShareDialogFragmentBinding;

import com.base.common.utils.DensityUtil;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.base.BaseActivity;
import com.base.common.view.base.BaseDialogFragment;
import com.base.common.viewmodel.BaseViewModel;

import com.rex.editor.common.Utils;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.io.File;
import java.util.List;

public class ShareDialogFragment extends BaseDialogFragment<ShareDialogFragmentBinding, BaseViewModel> {
    private ShareHelper.ShareBuilder builder;


    private static ShareDialogFragment newInstance(ShareHelper.ShareBuilder builder) {
        Bundle args = new Bundle();
        ShareDialogFragment fragment = new ShareDialogFragment();
        fragment.setArguments(args);
        fragment.setBuilder(builder);
        return fragment;
    }


    /**
     * 文章详情
     *
     * @param context
     * @param title
     * @param url
     * @param onClickChecked 新增举报 收藏  编辑    按钮功能
     * @return
     */
    public static ShareDialogFragment shareArticleNew(BaseActivity context, boolean showEdit, String title, String contents, String url, OnClickCheckedUtil onClickChecked) {
        List<String> list = Utils.getImageUrl(contents);
        String image = "";
        if (!UIUtils.isEmpty(list)) {
            image = list.get(0);
        }
        String str = Utils.getHtml2Text(contents);

        return ShareDialogFragment.newInstance(new ShareHelper.LinkBuilder(context, str)
                .title(title)
                .setAdditional(true)
                .setShowEdit(showEdit)
                .description(str)
                .icon(image)
                .setClickListener(onClickChecked));
    }

    /**
     *  分享链接
     * @param context
     * @param showEdit
     * @param title
     * @param contents
     * @param url
     * @param onClickChecked
     * @return
     */
    public static ShareDialogFragment shareUrl(BaseActivity context, boolean showEdit, String title, String contents, String url, OnClickCheckedUtil onClickChecked)
    {
        List<String> list = Utils.getImageUrl(contents);
        String image = "";
        if (!UIUtils.isEmpty(list))
        {
            image = list.get(0);
        }
        String str = Utils.getHtml2Text(contents);

        return ShareDialogFragment.newInstance(new ShareHelper.LinkBuilder(context, str)
                .title(title)
                .setAdditional(false)
                .setShowEdit(showEdit)
                .description(str)
                .icon(image)
                .setClickListener(onClickChecked));
    }

    /**
     *
     * @param context
     * @param title
     * @param contents
     * @param url
     * @param iconImg
     * @param onClickChecked
     * @return
     */
    public static ShareDialogFragment shareUrl(BaseActivity context,String title,String contents,String url,String iconImg,OnClickCheckedUtil onClickChecked)
    {
        String str = Utils.getHtml2Text(contents);
        return ShareDialogFragment.newInstance(new ShareHelper.LinkBuilder(context, url)
                .title(title)
                .setAdditional(false)
                .setShowEdit(false)
                .description(str)
                .icon(UIUtils.isNotEmpty(iconImg)?iconImg:"")
                .setClickListener(onClickChecked));
    }


    /**
     * 视频分享
     */
    public static ShareDialogFragment shareVideo(BaseActivity context, String videoUrl, String posterUrl, String title, String desc) {
        return ShareDialogFragment.newInstance(new ShareHelper.VideoBuilder(context, videoUrl, posterUrl).title(title).desc(desc));
    }

    /**
     * 图片分享 ``
     */
    public static ShareDialogFragment shareImage(BaseActivity context, File file) {
        return ShareDialogFragment.newInstance(new ShareHelper.ImageBuilder(context, file));
    }

    public static ShareDialogFragment shareImage(BaseActivity context, Bitmap bitmap) {
        return ShareDialogFragment.newInstance(new ShareHelper.ImageBuilder(context, bitmap));
    }

    public void setBuilder(ShareHelper.ShareBuilder builder) {
        this.builder = builder;
    }


    @Override
    protected ShareDialogFragmentBinding initDataBinding(LayoutInflater inflater, ViewGroup container) {
        return DataBindingUtil.inflate(inflater, R.layout.share_dialog_fragment, container, false);
    }

    @Override
    public void initView() {
        super.initView();
        if (builder instanceof ShareHelper.LinkBuilder) {
            ShareHelper.LinkBuilder linkBuilder = (ShareHelper.LinkBuilder) builder;
            if (linkBuilder.isAdditional()) {
                binding.vBottomLine.setVisibility(View.VISIBLE);
                binding.llBottom.setVisibility(View.VISIBLE);
                binding.llReport.setOnClickListener(new OnClickCheckedUtil() {
                    @Override
                    public void onClicked(View view) {
                        if (builder != null && builder.getClickListener() != null) {
                            UIUtils.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    builder.getClickListener().onClick(view);
                                }
                            }, 400);
                        }
                        finish();
                    }
                });


                if (linkBuilder.isShowEdit()) {
                    binding.llEditor.setVisibility(View.VISIBLE);
                    binding.llEditor.setOnClickListener(new OnClickCheckedUtil() {
                        @Override
                        public void onClicked(View view) {
                            if (builder != null && builder.getClickListener() != null) {
                                UIUtils.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        builder.getClickListener().onClick(view);
                                    }
                                }, 400);
                            }
                            finish();
                        }
                    });
                }


            } else {
                binding.vBottomLine.setVisibility(View.GONE);
                binding.llBottom.setVisibility(View.GONE);
            }
        }


        binding.btnCancel.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                mActivity.getSupportFragmentManager().beginTransaction().remove(ShareDialogFragment.this).commit();
            }
        });

        binding.btnWx.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                if (builder != null) {
                    builder.platform(SHARE_MEDIA.WEIXIN)
                            .build()
                            .share();
                }
                finish();
            }
        });

        binding.btnWxFriend.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                if (builder != null) {
                    builder.platform(SHARE_MEDIA.WEIXIN_CIRCLE)
                            .build()
                            .share();
                }
                finish();
            }
        });

        binding.btnQQ.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                if (builder != null) {
                    if(builder instanceof ShareHelper.LinkBuilder)
                    {
                        ((ShareHelper.LinkBuilder)builder).getShareLink().replace("/m-WeiXin","");
                    }

                    builder.platform(SHARE_MEDIA.QQ)
                            .build()
                            .share();
                }
                finish();
            }
        });

        binding.btnQZone.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                if (builder != null) {
                    builder.platform(SHARE_MEDIA.QZONE)
                            .build()
                            .share();
                }
                finish();
            }
        });

        binding.btnSina.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                if (builder != null) {
                    builder.platform(SHARE_MEDIA.SINA)
                            .build()
                            .share();
                }
                finish();
            }
        });
        //先屏蔽 其他按钮
        binding.btnQZone.setVisibility(View.GONE);
        binding.btnSina.setVisibility(View.GONE);
        binding.vBottomLine.setVisibility(View.GONE);
        binding.llBottom.setVisibility(View.GONE);

    }

    @Override
    protected int getHeight() {
        if (builder instanceof ShareHelper.LinkBuilder) {
            ShareHelper.LinkBuilder linkBuilder = (ShareHelper.LinkBuilder) builder;
            if (linkBuilder.isAdditional()) {
//                return UIUtils.getDimens(R.dimen.dp_300);
                return -2;
            }
        }
        return DensityUtil.getDimens(R.dimen.dp_200);
    }

    @Override
    public void initData() {
        super.initData();
    }
}
